package com.bii.gelapp;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import org.achartengine.GraphicalView;
import org.opencv.android.Utils;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class DNACalculateActivity extends Activity {
	double[] distance;
	double[] gelvalues;
	double[] componentdistance;
	double[] welldistances;
	double[] wellcomponentdistances;

	ArrayList<ConnectedComponent> componentlist = new ArrayList<ConnectedComponent>();
	ArrayList<ConnectedComponent> markerlist = new ArrayList<ConnectedComponent>();
	public static ArrayList markervalues = new ArrayList();
	public static ArrayList componentvalues;
	
	ImageView ivImage, ivGraph;
	Bitmap bmap;
	Mat drawmat;
	Bitmap colorBmp;
	Button btGraphExport;
	Bitmap graphbmp;
	//estimated distance from well
	public static double edw;
	public static double topmarkercenter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dnacalculateactivity);
		componentvalues = new ArrayList();
		componentlist = (ArrayList<ConnectedComponent>) getIntent()
				.getSerializableExtra("componentlist");
		markerlist = (ArrayList<ConnectedComponent>) getIntent()
				.getSerializableExtra("markerlist");
		markervalues = (ArrayList)getIntent().getSerializableExtra("markervalues");
		
		distance = new double[markerlist.size()];
		gelvalues = new double[markerlist.size()];
		componentdistance = new double[componentlist.size()];
		welldistances = new double[markerlist.size()];
		wellcomponentdistances = new double[componentlist.size()];
		int secondmarkervalue = (Integer) markervalues.get(1);
		int firstmarkervalue = (Integer) markervalues.get(0);
		
		double distbetweensectop = ((markerlist.get(1).bottom + markerlist.get(1).top) / 2)
				- ((markerlist.get(0).bottom + markerlist.get(0).top) / 2);
		edw = (Math.log10(secondmarkervalue) * distbetweensectop)
				/ (Math.log10(firstmarkervalue) - (Math
						.log10(secondmarkervalue)));
		
		for (int i = 0; i < markerlist.size(); i++) {
			if (i == 0) {
				//first one's distance would be from the well, so its just edw
				welldistances[i] = edw;
			} else {
				double distbetweenfrag = ((markerlist.get(i).bottom + markerlist
						.get(i).top) / 2)
						- ((markerlist.get(i - 1).bottom + markerlist
								.get(i - 1).top) / 2);
				double wd = welldistances[i - 1] + distbetweenfrag;
				welldistances[i] = wd;
			}
		}
		
		for (int i = 0; i < componentlist.size(); i++) {
			double distancetopmarker = ((componentlist.get(i).bottom + componentlist
					.get(i).top) / 2)
					- ((markerlist.get(0).bottom + markerlist.get(0).top) / 2);
			double wcd = distancetopmarker + edw;
			

			wellcomponentdistances[i] = wcd;
			Log.d("tag1", "wellcomponentdistances " + i + " : "
					+ wellcomponentdistances[i]);
		}
		//topmarkercenter = ((markerlist.get(0).bottom + markerlist.get(0).top) / 2);
		
		for (int i = 0; i < markerlist.size(); i++) {
			//gelvalues[i] = Math.log10((double) mss.getmarkers().get(i).value);
			int gelvalue = (Integer) markervalues.get(i);
			double gelvaluetoadd = (double)gelvalue;
			gelvalues[i] = Math.log10(gelvaluetoadd);
			
		}
		
		ScatterGraph scatter = new ScatterGraph();
		GraphicalView graphview = scatter.getGraphicalView(
				DNACalculateActivity.this, welldistances, gelvalues,
				wellcomponentdistances, "DNA");
		LinearLayout graphcontainer = (LinearLayout) findViewById(R.id.graphcontainer);
		graphcontainer.addView(graphview);
		graphbmp = graphview.toBitmap();
		
		ivImage = (ImageView) findViewById(R.id.ivImage);
		colorBmp = RotationActivity.colorBmp;

		componentlist = (ArrayList<ConnectedComponent>) getIntent()
				.getSerializableExtra("componentlist");
		markerlist = (ArrayList<ConnectedComponent>) getIntent()
				.getSerializableExtra("markerlist");

		bmap = colorBmp;
		drawmat = new Mat(bmap.getHeight(), bmap.getWidth(), CvType.CV_8U);

		drawRectangles();
		//Bitmap drawnbmp = Bitmap.createBitmap(drawmat.cols(), drawmat.rows(),
		//		Bitmap.Config.RGB_565);
		//Utils.matToBitmap(drawmat, drawnbmp);

		//Bitmap resizedbmp = Bitmap.createScaledBitmap(drawnbmp, 300, 300, true);
		//ivImage.setImageBitmap(resizedbmp);
		//ivImage.setImageBitmap(drawnbmp);
	}
	
	private void drawRectangles() {
		Utils.bitmapToMat(bmap, drawmat);
		for (int i = 0; i < componentlist.size(); i++) {
			Core.rectangle(drawmat, new Point(componentlist.get(i).left,
					componentlist.get(i).top), new Point(
					componentlist.get(i).right, componentlist.get(i).bottom),
					new Scalar(255, 0, 0), 2);
			// 11/8
			int top = componentlist.get(i).top;
			int bottom = componentlist.get(i).bottom;
			int left = componentlist.get(i).left;
			int right = componentlist.get(i).right;

			int centerheight = (bottom + top) / 2;
			int centerwidth = (right + left) / 2;
			Point org = new Point(centerwidth, centerheight);

			Core.putText(drawmat, "" + i, org, 3, 1, new Scalar(0, 0, 255), 5);
			// Core.putText(img, text, org, fontFace, fontScale, color);
		}
		for (int i = 0; i < markerlist.size(); i++) {
			Core.rectangle(drawmat, new Point(markerlist.get(i).left,
					markerlist.get(i).top), new Point(markerlist.get(i).right,
					markerlist.get(i).bottom), new Scalar(255, 255, 0), 2);
			int top = markerlist.get(i).top;
			int bottom = markerlist.get(i).bottom;
			int left = markerlist.get(i).left;
			int right = markerlist.get(i).right;

			int centerheight = (bottom + top) / 2;
			Point org = new Point(right, centerheight);

			Core.putText(drawmat, "" + i, org, 3, 1, new Scalar(0, 255, 0), 5);
		}
		Bitmap drawnbmp = Bitmap.createBitmap(drawmat.cols(), drawmat.rows(),
				Bitmap.Config.RGB_565);
		Utils.matToBitmap(drawmat, drawnbmp);

		//Bitmap resizedbmp = Bitmap.createScaledBitmap(drawnbmp, 300, 300, true);
		//ivImage.setImageBitmap(resizedbmp);
		ivImage.setImageBitmap(drawnbmp);

		ivImage.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent goToNextActivity = new Intent(getApplicationContext(),
						DNACalculateFullscreenActivity.class);
				try {
					InternalStorage.writeBitmap(DNACalculateActivity.this, "colorBmp", colorBmp);
					goToNextActivity.putExtra("componentvalues", componentvalues);
					goToNextActivity.putExtra("componentlist", componentlist);
					goToNextActivity.putExtra("markerlist", markerlist);
					goToNextActivity.putExtra("markervalues", markervalues);
					startActivity(goToNextActivity);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				

			}
		});

	};

	public byte[] compressImage(Bitmap image) {
		ByteArrayOutputStream stream = new ByteArrayOutputStream();
		image.compress(Bitmap.CompressFormat.PNG, 0, stream);
		byte[] bytes = stream.toByteArray();
		return bytes;
	}
}
